package com.xiaohe.gulimall.coupon.service.impl;

import com.xiaohe.gulimall.coupon.entity.SeckillSkuRelationEntity;
import com.xiaohe.gulimall.coupon.service.SeckillSkuRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaohe.common.utils.PageUtils;
import com.xiaohe.common.utils.Query;

import com.xiaohe.gulimall.coupon.dao.SeckillSessionDao;
import com.xiaohe.gulimall.coupon.entity.SeckillSessionEntity;
import com.xiaohe.gulimall.coupon.service.SeckillSessionService;


@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSessionEntity> implements SeckillSessionService {


    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillSessionEntity> page = this.page(
                new Query<SeckillSessionEntity>().getPage(params),
                new QueryWrapper<SeckillSessionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<SeckillSessionEntity> getLate3DaySession() {

        QueryWrapper<SeckillSessionEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("start_time",startTime(),endTime());
        //计算最近三天的时间
        List<SeckillSessionEntity> list = this.list(queryWrapper);
        if (list!=null&& list.size()>0){
            List<SeckillSessionEntity> collect = list.stream().map(session -> {
                Long id = session.getId();
                QueryWrapper<SeckillSkuRelationEntity> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("promotion_session_id", id);
                List<SeckillSkuRelationEntity> list1 = seckillSkuRelationService.list(queryWrapper1);
                session.setRelationSkus(list1);
                return session;
            }).collect(Collectors.toList());
            return collect;
        }
        return null;
    }

    public String startTime(){
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        LocalDateTime start = LocalDateTime.of(now,min);
        String format = start.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        return format;
    }

    public  String endTime(){
        LocalDate now = LocalDate.now();
        LocalDate plus = now.plusDays(2);
        LocalTime max = LocalTime.MAX;
        LocalDateTime end = LocalDateTime.of(plus, max);
        String format = end.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        System.out.println(format);
        return format;
    }

    public static void main(String[] args) {
        SeckillSessionServiceImpl seckillSessionService = new SeckillSessionServiceImpl();
        seckillSessionService.endTime();
    }
}